<template>
  <div
    class="page_body"
    v-loading.fullscreen.lock="loadingKey"
    :element-loading-text="loadingText"
  >
    <!-- 面包屑 -->
    <el-row>
      <el-col :span="24" class="page_box_top">
        <i class="el-icon-s-fold"></i>
        <el-breadcrumb separator="/" class="page_box_top_nav">
          <el-breadcrumb-item class="breadcrumb_active_item"
            >订单管理</el-breadcrumb-item
          >
          <el-breadcrumb-item>机构订单</el-breadcrumb-item>
        </el-breadcrumb>
      </el-col>
    </el-row>
    <el-row>
      <div class="organization_order_content">
        <el-row>
          <el-col :span="24" style="display: flex; align-items: center">
            <div class="stu_total_count_title">考生总数：</div>
            <div class="stu_total_count">{{ stuTotalCount }} 人</div>
          </el-col>
        </el-row>
        <!-- 搜索框 -->

        <el-row>
          <el-col :span="24">
            <div class="organization_order_search">
              <div>筛选：</div>
              <el-select
                v-model="checkedLevel"
                placeholder="请选择考试等级"
                size="mini"
                style="width: 150px"
                @change="checkedLevelChange"
                :clearable="true"
              >
                <el-option
                  v-for="item in levelList"
                  :key="item.key"
                  :label="item.value"
                  :value="item.key"
                >
                </el-option>
              </el-select>
              <el-select
                v-model="checkdeAreaId"
                placeholder="请选择考点"
                size="mini"
                style="width: 150px; margin-left: 10px"
                @change="checkedAreaIdChange"
                :clearable="true"
              >
                <el-option
                  v-for="item in areaList"
                  :key="item.key"
                  :label="item.value"
                  :value="item.key"
                >
                </el-option>
              </el-select>
              <el-select
                v-model="checkedExam"
                placeholder="请选择考试科目"
                size="mini"
                @change="checkedExamChange"
                :clearable="true"
                style="width: 150px; margin-left: 10px"
              >
                <el-option
                  v-for="item in checkedExamList"
                  :key="item.key"
                  :label="item.value"
                  :value="item.key"
                >
                </el-option>
              </el-select>
              <el-select
                v-model="checkedOrderStatus"
                placeholder="请选择订单状态"
                size="mini"
                style="width: 150px; margin-left: 10px"
                @change="checkedOrderStatusChange"
                :clearable="true"
              >
                <el-option
                  v-for="item in orderStatusList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
              <el-input
                v-model="searchKey"
                placeholder="请输入关键词"
                style="width: 150px; margin-left: 10px"
                size="mini"
              ></el-input>
              <el-button
                type="primary"
                size="mini"
                style="margin-left: 5px"
                @click="searchFn()"
                >搜索</el-button
              >
            </div>
          </el-col>
        </el-row>
        <!-- 数据列表 -->
        <el-row
          type="flex"
          justify="center"
          v-if="orderListData != undefined && orderListData.length > 0"
        >
          <el-col :span="24">
            <el-table
              :data="orderListData"
              border
              style="width: 100%"
              size="mini"
              :cell-style="cellStyleFn"
            >
              <el-table-column
                type="index"
                label="序号"
                width="50"
                align="center"
              >
              </el-table-column>

              <el-table-column
                prop="order_number"
                label="订单编号"
                width="220"
                align="center"
              ></el-table-column>

              <el-table-column
                prop="title"
                label="考试项目"
                align="center"
                min-width="220"
              ></el-table-column>

              <el-table-column
                prop="exam_level"
                label="考试项目级别"
                align="center"
                width="110"
                :formatter="formatterLeveFn"
              ></el-table-column>

              <el-table-column
                prop="place"
                label="考试地点"
                width="160"
                align="center"
              ></el-table-column>

              <el-table-column
                prop="name"
                label="机构名称"
                width="160"
                align="center"
              ></el-table-column>

              <el-table-column
                prop="created_at"
                label="创建时间"
                align="center"
                width="180"
              ></el-table-column>

              <el-table-column
                prop="single_exam_money"
                label="考试费用"
                width="100"
                align="center"
              ></el-table-column>

              <el-table-column
                prop="stu_count"
                label="考试人数"
                width="90"
                align="center"
              ></el-table-column>

              <el-table-column
                prop="exam_money"
                label="考试费用总计"
                width="120"
                align="center"
              ></el-table-column>

              <el-table-column
                prop="order_status"
                label="订单状态"
                width="120"
                align="center"
                :formatter="formatterStatusFn"
              ></el-table-column>

              <el-table-column
                label="操作"
                width="200"
                itemprop=""
                align="center"
              >
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="primary"
                    @click="detailFn(scope.$index, scope.row)"
                    >查看</el-button
                  >
                  <el-button
                    size="mini"
                    type="primary"
                    @click="orderComplete(scope.$index, scope.row)"
                    v-if="scope.row.order_status == 1"
                    >确认支付</el-button
                  >
                  <el-button
                    size="mini"
                    type="info"
                    @click="orderRollback(scope.$index, scope.row)"
                    v-if="scope.row.order_status == 2"
                    >取消确认</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
        <el-row
          type="flex"
          justify="center"
          v-if="orderListData != undefined && orderListData.length > 0"
        >
          <el-col :span="24">
            <div class="organization_order_pagination">
              <el-pagination
                @size-change="listSizeChange"
                @current-change="listCurrentChange"
                :current-page="currentPage"
                :page-sizes="pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
              >
              </el-pagination>
            </div>
          </el-col>
        </el-row>
        <el-row v-else>
          <el-empty description="暂无订单"></el-empty>
        </el-row>
      </div>
    </el-row>
  </div>
</template>
<style src="../../assets/css/order/organizationOrder.css" scoped></style>

<script>
export default {
  name: "OrganizationOrder",
  components: {},
  data() {
    return {
      loadingKey: false,
      loadingText: "",
      orderListData: [], // 订单列表数据
      currentPage: 1, // 当前页
      pageSizes: [10, 20, 50, 100], // 可选页容量
      pageSize: 10, // 当前页容量
      total: 0, // 总条数
      searchKey: "", //搜索关键词
      checkedLevel: "", // 筛选-篮球等级
      levelList: [], // 等级列表
      orderStatusList: [
        {
          value: 1,
          label: "待支付",
        },
        {
          value: 2,
          label: "已支付",
        },
        {
          value: 6,
          label: "已退款",
        },
        {
          value: 7,
          label: "退款中",
        },
      ],
      checkedOrderStatus: "", // 筛选-订单状态
      checkedExam: "", // 筛选-科目
      checkedExamList: [], // 科目列表
      stuTotalCount: 0, // 考生总数
      checkdeAreaId: "", // 筛选--考点
      areaList: [], // 考点列表
    };
  },
  created: function () {
    this.getlevelListFn();
    this.getListDataFn();
    this.getCheckedExamListFn();
    this.getAreaListFn();
  },
  methods: {
    // 搜索按钮
    searchFn: function () {
      this.currentPage = 1;
      this.getListDataFn();
    },
    // 考点筛选
    checkedAreaIdChange: function (val) {
      this.checkdeAreaId = val;
      this.getCheckedExamListFn();
    },
    // 等级筛选
    checkedLevelChange: function (val) {
      this.checkedLevel = val;
      this.getCheckedExamListFn();
    },
    // 订单状态筛选
    checkedOrderStatusChange: function (val) {
      this.checkedOrderStatus = val;
    },
    // 科目筛选
    checkedExamChange: function (val) {
      this.checkedExam = val;
    },
    // 获取考点列表数据
    getAreaListFn: function () {
      let that = this;
      let url = "/admin/area/index";
      let data = {};
      that.loadingText = "加载中...";
      that.loadingKey = true;
      this.axios
        .post(url, data)
        .then((response) => {
          that.loadingKey = false;
          if (response.code == 0) {
            let list = [];
            response.area_list.forEach(function (item) {
              let obj = {
                key: item.id,
                value: item.area,
              };
              list.push(obj);
            });
            that.areaList = list;
          } else {
            that.$message.error(response.msg);
          }
        })
        .catch(function (error) {
          that.loadingKey = false;
          console.log(error);
        });
    },
    // 获取等级列表数据
    getlevelListFn: function () {
      let that = this;
      let url = "/mobile/levelText";
      let data = {};
      that.loadingText = "加载中...";
      that.loadingKey = true;
      this.axios
        .post(url, data)
        .then((response) => {
          that.loadingKey = false;
          if (response.code == 0) {
            let list = [];
            response.level_list.forEach(function (item) {
              let obj = {
                key: item.level,
                value: item.title,
              };
              list.push(obj);
            });
            that.levelList = list;
          } else {
            that.$message.error(response.msg);
          }
        })
        .catch(function (error) {
          that.loadingKey = false;
          console.log(error);
        });
    },
    // 查询列表数据
    getListDataFn: function () {
      let that = this;
      let url = "/admin/order/index";
      let data = {
        keywords: this.searchKey,
        page: this.currentPage,
        perPage: this.pageSize,
        type: 2,
        level: this.checkedLevel,
        order_status: this.checkedOrderStatus,
        exam_id: this.checkedExam,
        area_id: this.checkdeAreaId,
      };
      this.axios
        .post(url, data)
        .then((response) => {
          if (response.code == 0) {
            that.orderListData = response.orderinfo;
            that.total = response.total_count;
            that.stuTotalCount = response.stu_total_count;
          } else {
            that.$message.error(response.msg);
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    // 获取考试科目列表数据
    getCheckedExamListFn: function () {
      let that = this;
      let url = "/admin/exam/examQueryCondition";
      let data = {
        level: this.checkedLevel,
        area_id: this.checkdeAreaId,
      };
      that.loadingText = "加载中...";
      that.loadingKey = true;
      this.axios
        .post(url, data)
        .then((response) => {
          that.loadingKey = false;
          if (response.code == 0) {
            let list = [];
            response.exam_list.forEach(function (item) {
              let obj = {
                key: item.id,
                value: item.title,
              };
              list.push(obj);
            });
            that.checkedExamList = list;
          } else {
            that.$message.error(response.msg);
          }
        })
        .catch(function (error) {
          that.loadingKey = false;
          console.log(error);
        });
    },
    // 设置单元格字体颜色
    cellStyleFn: function ({ row, columnIndex }) {
      let style = "";
      if (columnIndex == 7 || columnIndex == 9) {
        style = "color: #F6705E";
      }
      if (columnIndex == 10) {
        if (row.order_status == 1 || row.order_status == 6) {
          style = "color: #F6705E";
        } else {
          style = "color: #8ECF5F";
        }
      }
      return style;
    },
    // 订单状态格式化
    formatterStatusFn: function (row) {
      let orderStatus = row.order_status;
      let text = "";
      this.orderStatusList.forEach(function (item) {
        if (item.value == orderStatus) {
          text = item.label;
        }
      });
      return text;
    },
    // 等级格式化
    formatterLeveFn: function (row) {
      let exam_level = row.exam_level;
      let text = "";
      this.levelList.forEach(function (item) {
        if (item.key == exam_level) {
          text = item.value;
        }
      });
      return text;
    },
    // 页容量改变
    listSizeChange: function (val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.getListDataFn();
    },
    // 当前页改变
    listCurrentChange: function (val) {
      this.currentPage = val;
      this.getListDataFn();
    },
    // 查看详情
    detailFn: function (index, row) {
      const routeUrl = this.$router.resolve({
        name: "OrderDetail",
      });
      let params = {
        id: row.id,
        key: 2,
      };
      this.$store.commit("set_orderDetailParams", params);
      window.open(routeUrl.href, "_blank");
    },
    // 确认完成
    orderComplete: function (index, row) {
      let that = this;
      let url = "/admin/order/complete";
      let data = {
        order_id: row.id,
      };
      this.$confirm("确认该订单已完成支付?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        that.loadingText = "订单处理中...";
        that.loadingKey = true;
        that.axios
          .post(url, data)
          .then((response) => {
            that.loadingKey = false;
            if (response.code == 0) {
              that.$message.success("操作成功");
              that.currentPage = 1;
              that.getListDataFn();
            } else {
              that.$message.error(response.msg);
            }
          })
          .catch(function (error) {
            that.loadingKey = false;
            console.log(error);
          });
      });
    },
    // 取消完成
    orderRollback: function (index, row) {
      let that = this;
      let url = "/admin/order/rollback";
      let data = {
        order_id: row.id,
      };
      this.$confirm("确认取消该订单完成状态?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        that.loadingText = "订单处理中...";
        that.loadingKey = true;
        that.axios
          .post(url, data)
          .then((response) => {
            that.loadingKey = false;
            if (response.code == 0) {
              that.$message.success("操作成功");
              that.currentPage = 1;
              that.getListDataFn();
            } else {
              that.$message.error(response.msg);
            }
          })
          .catch(function (error) {
            that.loadingKey = false;
            console.log(error);
          });
      });
    },
  },
};
</script>
